#pragma once

namespace dotNetInteropPlugin {
	namespace PluginDev {
		using namespace System;
		using namespace System::Collections;
		using namespace System::Collections::Generic;

		//typedef System::Collections::ObjectModel::Collection<RootMenuItem> CollectionBase;
		typedef dotNetInteropPlugin::PluginDev::Additions::MBRCollection<RootMenuItem> CollectionBase;

		private ref class MAIMPMenuManager: public CollectionBase, public IMAIMPMenuManager, public System::IDisposable
		{
		public:
			MAIMPMenuManager(IAIMP2Controller* AIMPCtrl)
			{
				inAIMPCtrl = AIMPCtrl;
				inAIMPCtrl->AddRef();
			}

			~MAIMPMenuManager()
			{
				if (inAIMPCtrl != NULL)
				{
					this->Clear();
					inAIMPCtrl->Release();
					inAIMPCtrl = NULL;
				}
			}

			virtual void Add(AIMPPlayerMenu MN, MAIMPMenuItem^ MI)
			{
				this->Add(RootMenuItem(MN, MI));
			}

		protected:
			virtual void SetItem(Int32 index, RootMenuItem val) override
			{
				throw gcnew System::NotImplementedException();
			}
			virtual void InsertItem(Int32 index, RootMenuItem val) override
			{
				if (val.MenuItem->Register(inAIMPCtrl, val.PlayerMenu))
					CollectionBase::InsertItem(index, val);
			}

			virtual void ClearItems() override
			{				
				for (int index = 0; index < this->Count; index++)
					this->default[index].MenuItem->Unregister();
				CollectionBase::ClearItems();
			}
			virtual void RemoveItem(Int32 index) override
			{
				if (index > 0 && index < this->Count)
					this->default[index].MenuItem->Unregister();
				CollectionBase::RemoveItem(index);
			}

		private:
			IAIMP2Controller* inAIMPCtrl;
		};

	}
}